define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tryride/index' + location.search,
                    add_url: 'tryride/add',
                    edit_url: 'tryride/edit',
                    del_url: 'tryride/del',
                    multi_url: 'tryride/multi',
                    import_url: 'tryride/import',
                    table: 'tryride',
                }
            });

            var table = $("#table");

            Fast.config.openArea = ['90%','80%'];

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'createtime',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'applyuid', title: __('Applyuid')},
                        {field: 'touid', title: __('Touid')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'startTime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'endTime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'time_unit', title: __('Time_unit')},
                        {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'evaluation', title: __('Evaluation')},
                        {field: 'push_result_shenqingren', title: __('申请人通知结果'), operate: false},
                        {field: 'push_result_beishenqingren', title: __('被申请人通知结果'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        userlist: function () {
            let ids = location.pathname.split("/").slice(-1);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    userlist_url: 'tryride/userlist/ids/' + ids
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.userlist_url,
                escape: false,
                search: false,
                commonSearch:false,
                pk: 'id',
                sortName: 'endTime',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'applyname', title: __('Applyuid')},
                        {field: 'toname', title: __('Touid')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'startTime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'endTime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'time_unit', title: __('Time_unit')},
                        {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, sortable: true},
                        {field: 'evaluation', title: __('Evaluation')}
                    ]
                ]
            });
           // 为表格绑定事件
          Table.api.bindevent(table); 
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tryride/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'applyname', title: __('Applyuid')},
                        {field: 'toname', title: __('Touid')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tryride/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tryride/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-applyuid").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {uid: $("#c-touid").val(),sqr: 1}};
            });
            $("#c-touid").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {uid: $("#c-applyuid").val(),sqr: 0}};
            });
            $(document).on("change", "#c-applyuid", function(){
                var uid = $('#c-applyuid').val();
                console.log(uid);
                if (uid) {
                    Fast.api.ajax({
                        url: 'tryride/getuserinfo',
                        data: {'uid':uid,'sqr':1}
                    }, function (data) {
                        $('#c-applymobile').val(data.tryrideinfo.mobile);
                        $('#c-applyaddress').val(data.tryrideinfo.address);
                    })
                }
                
            });
            $(document).on("change", "#c-touid", function(){
                var uid = $('#c-touid').val();
                if (uid) {
                    Fast.api.ajax({
                        url: 'tryride/getuserinfo',
                        data: {'uid':uid,'sqr':0}
                    }, function (data) {
                        $('#c-tomobile').val(data.tryrideinfo.mobile);
                        $('#c-toaddress').val(data.tryrideinfo.address);
                    })
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-applyuid").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {uid: $("#c-touid").val(),sqr: 1}};
            });
            $("#c-touid").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {uid: $("#c-applyuid").val(),sqr: 0}};
            });
            $(document).on("change", "#c-applyuid", function(){
                var uid = $('#c-applyuid').val();
                Fast.api.ajax({
                    url: 'tryride/getuserinfo',
                    data: {'uid':uid,'sqr':1}
                }, function (data) {
                    $('#c-applymobile').val(data.tryrideinfo.mobile);
                    $('#c-applyaddress').val(data.tryrideinfo.address);
                })
            });
            $(document).on("change", "#c-touid", function(){
                var uid = $('#c-touid').val();
                Fast.api.ajax({
                    url: 'tryride/getuserinfo',
                    data: {'uid':uid,'sqr':0}
                }, function (data) {
                    $('#c-tomobile').val(data.tryrideinfo.mobile);
                    $('#c-toaddress').val(data.tryrideinfo.address);
                })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
